<?php


namespace App\Http\Controllers\Sesame;


use App\Patent;
use App\Setting;
use Carbon\Carbon;
use Illuminate\Http\Request;

class SyncApiController extends ApiController
{

    public function index(Request $request)
    {
        $list = Patent::query()
            ->whereIn('patent_type_id', [1, 2, 3])
            ->whereIn('state',[0,1])
            ->pluck('patent_sn')->toArray();
        return $this->success($list);
    }

    public function updateBatch(Request $request)
    {
        $data = $request->get('data');
        if (!is_array($data)) {
            return $this->error('参数错误');
        }
        foreach ($data as $item) {
            $detail = collect($item)->only('patent_sn', 'patent_name', 'applicant', 'inventor', 'ipc_main', 'legal_status', 'publication_num', 'publication_date', 'apply_date');
            $patent_sn = $detail->get('patent_sn');
            $model = $patent_sn ? Patent::query()->where('patent_sn', $patent_sn)->first() : null;
            if ($model instanceof Patent) {
                if ($detail->get('legal_status')) {
                    $this->syncDetail($model, $detail->all());
                }
                $djf = data_get($item,'djf',[]);
                $yjf = data_get($item,'yjf',[]);
                $this->syncFee($model, $djf, $yjf);
            }
        }
        return $this->success('ok');
    }

    protected function syncDetail(Patent $model, $data)
    {
        $model->applicant = data_get($data, 'applicant') ?: $model->applicant;
        if (!$model->patent_person && $model->applicant) {
            $model->patent_person = collect(explode(',', $model->applicant))->first();
        }
        $model->inventor = $model->inventor ?: data_get($data, 'inventor');
        $model->ipc_main = $model->ipc_main ?: data_get($data, 'ipc_main');
        $model->publication_num = $model->publication_num ?: data_get($data, 'publication_num');
        $model->publication_date = $model->publication_date ?: data_get($data, 'publication_date');
        $model->legal_status = $data['legal_status'];
        $model->save();
    }

    protected function syncFee(Patent $model, $djf, $yjf)
    {
        $data = array_merge($djf, $yjf);
        $data = collect($data)->map(function ($item) use ($model) {
            $pay_time = data_get($item, 'pay_time');
            return [
                'user_id' => $model['user_id'],
                'patent_sn' => $model['patent_sn'],
                'name' => $item['name'],
                'amount' => $item['amount'],
                'last_time' => data_get($item, 'last_time') ?: null,
                'payer' => data_get($item, 'payer') ?: null,
                'pay_time' => $pay_time ?: null,
                'bill_code' => data_get($item, 'bill_code') ?: null,
                'bill_number' => data_get($item, 'bill_number') ?: null,
                'state' => $pay_time ? 1 : 0,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
            ];
        })->values()->all();
        if ($data) {
            $model->fees()->delete();
            $model->fees()->createMany($data);
        }
    }
}
